<!-- 积分设置->列表 -->
<template>
  <div class="setBox">
    <el-form ref="form" :model="form" label-width="170px">
      <div style="margin-left:12px">
        <span>打包任务奖励积分</span>
        <el-input v-model="form.pack" min="0" max="999999999" oninput="value=value.replace(/^\.+|[^\d.]/g,'')" clearable type="number" @keyup.native="form.pack = checkInput(form.pack)" />
        <span>积分/单</span>
      </div>
      <div style="margin-left:12px">
        <span>贴单任务奖励积分</span>
        <el-input v-model="form.paste" min="0" max="999999999" oninput="value=value.replace(/^\.+|[^\d.]/g,'')" clearable type="number" @keyup.native="form.paste = checkInput(form.paste)" />
        <span>积分/单</span>
      </div>
      <div style="margin-left:-28px">
        <span>打包+贴单任务奖励积分</span>
        <el-input v-model="form.pack_paste" min="0" max="999999999" oninput="value=value.replace(/^\.+|[^\d.]/g,'')" clearable type="number" @keyup.native="form.pack_paste = checkInput(form.pack_paste)" />
        <span>积分/单</span>
      </div>
      <div>
        <span>日排名第一奖励积分</span>
        <el-input v-model="form.first" min="0" max="999999999" oninput="value=value.replace(/^\.+|[^\d.]/g,'')" clearable type="number" @keyup.native="form.first = checkInput(form.first)" />
        <span>积分</span>
      </div>
      <div>
        <span>日排名第二奖励积分</span>
        <el-input v-model="form.second" min="0" max="999999999" oninput="value=value.replace(/^\.+|[^\d.]/g,'')" clearable type="number" @keyup.native="form.second = checkInput(form.second)" />
        <span>积分</span>
      </div>
      <div>
        <span>日排名第三奖励积分</span>
        <el-input v-model="form.third" min="0" max="999999999" oninput="value=value.replace(/^\.+|[^\d.]/g,'')" clearable type="number" @keyup.native="form.third = checkInput(form.third)" />
        <span>积分</span>
      </div>
      <!-- <el-row>
        <el-col :span="16">
            <el-form-item label="打包任务奖励积分" prop="pack">
              <el-input v-model="form.pack" clearable t_value="" o_value="" onkeypress="if(!this.value.match(/^[\+\-]?\d*?\.?\d*?$/))this.value=this.t_value;else this.t_value=this.value;if(this.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?)?$/))this.o_value=this.value" onkeyup="if(!this.value.match(/^[\+\-]?\d*?\.?\d*?$/))this.value=this.t_value;else this.t_value=this.value;if(this.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?)?$/))this.o_value=this.value" onblur="if(!this.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?|\.\d*?)?$/))this.value=this.o_value;else{if(this.value.match(/^\.\d+$/))this.value=0+this.value;if(this.value.match(/^\.$/))this.value=0;this.o_value=this.value}">
                <template slot="append">积分/单</template>
              </el-input>
            </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="16">
          <el-form-item label="贴单任务奖励积分" prop="paste">
            <el-input  v-model="form.paste" clearable t_value="" o_value="" onkeypress="if(!this.value.match(/^[\+\-]?\d*?\.?\d*?$/))this.value=this.t_value;else this.t_value=this.value;if(this.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?)?$/))this.o_value=this.value" onkeyup="if(!this.value.match(/^[\+\-]?\d*?\.?\d*?$/))this.value=this.t_value;else this.t_value=this.value;if(this.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?)?$/))this.o_value=this.value" onblur="if(!this.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?|\.\d*?)?$/))this.value=this.o_value;else{if(this.value.match(/^\.\d+$/))this.value=0+this.value;if(this.value.match(/^\.$/))this.value=0;this.o_value=this.value}">
              <template slot="append">积分/单</template>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="16">
          <el-form-item label="打包+贴单任务奖励积分"  prop="packPaste">
            <el-input v-model="form.packPaste" t_value="" o_value="" onkeypress="if(!this.value.match(/^[\+\-]?\d*?\.?\d*?$/))this.value=this.t_value;else this.t_value=this.value;if(this.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?)?$/))this.o_value=this.value" onkeyup="if(!this.value.match(/^[\+\-]?\d*?\.?\d*?$/))this.value=this.t_value;else this.t_value=this.value;if(this.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?)?$/))this.o_value=this.value" onblur="if(!this.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?|\.\d*?)?$/))this.value=this.o_value;else{if(this.value.match(/^\.\d+$/))this.value=0+this.value;if(this.value.match(/^\.$/))this.value=0;this.o_value=this.value}" clearable>
              <template slot="append">积分/单</template>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="16">
          <el-form-item label="日排名第一奖励积分" prop="first">
            <el-input v-model="form.first" t_value="" o_value="" onkeypress="if(!this.value.match(/^[\+\-]?\d*?\.?\d*?$/))this.value=this.t_value;else this.t_value=this.value;if(this.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?)?$/))this.o_value=this.value" onkeyup="if(!this.value.match(/^[\+\-]?\d*?\.?\d*?$/))this.value=this.t_value;else this.t_value=this.value;if(this.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?)?$/))this.o_value=this.value" onblur="if(!this.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?|\.\d*?)?$/))this.value=this.o_value;else{if(this.value.match(/^\.\d+$/))this.value=0+this.value;if(this.value.match(/^\.$/))this.value=0;this.o_value=this.value}" clearable>
              <template slot="append">积分</template>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="16">
          <el-form-item label="日排名第二奖励积分" prop="second">
            <el-input v-model="form.second" t_value="" o_value="" onkeypress="if(!this.value.match(/^[\+\-]?\d*?\.?\d*?$/))this.value=this.t_value;else this.t_value=this.value;if(this.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?)?$/))this.o_value=this.value" onkeyup="if(!this.value.match(/^[\+\-]?\d*?\.?\d*?$/))this.value=this.t_value;else this.t_value=this.value;if(this.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?)?$/))this.o_value=this.value" onblur="if(!this.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?|\.\d*?)?$/))this.value=this.o_value;else{if(this.value.match(/^\.\d+$/))this.value=0+this.value;if(this.value.match(/^\.$/))this.value=0;this.o_value=this.value}" clearable>
              <template slot="append">积分</template>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="16">
          <el-form-item label="日排名第三奖励积分" prop="third">
            <el-input v-model="form.third" t_value="" o_value="" onkeypress="if(!this.value.match(/^[\+\-]?\d*?\.?\d*?$/))this.value=this.t_value;else this.t_value=this.value;if(this.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?)?$/))this.o_value=this.value" onkeyup="if(!this.value.match(/^[\+\-]?\d*?\.?\d*?$/))this.value=this.t_value;else this.t_value=this.value;if(this.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?)?$/))this.o_value=this.value" onblur="if(!this.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?|\.\d*?)?$/))this.value=this.o_value;else{if(this.value.match(/^\.\d+$/))this.value=0+this.value;if(this.value.match(/^\.$/))this.value=0;this.o_value=this.value}" clearable>
              <template slot="append">积分</template>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row> -->
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="funcSubmit">确定</el-button>
    </div>
  </div>
</template>
<script>
import Vue from 'vue'
import 'xe-utils'
import VXETable from 'vxe-table'
import 'vxe-table/lib/index.css'
import {pointsSettingQuery, pointsSettingSave} from "@/api/integralManage/integralSet";
import {pointsExchangeGoodsList} from "@/api/integralManage/redeemGift";
Vue.use(VXETable)

export default {
  name: 'Parent',
  components: {},
  data() {
    return {
      form: {
        pack: '0',
        paste: '0',
        pack_paste: '0',
        first: '0',
        second: '0',
        third: '0'
      },
      arrnew: []
    }
  },
  created() {
    this.from()
  },
  methods: {
    //获取积分设置
    from(){
      this.loading = true
      return pointsSettingQuery().then(response => {
        if(response.data.length === 0){
          return
        }else {
          let obj = {}
          for(var i = 0;i < response.data.length; i++){
            // 定义key
            let key = response.data[i].settingName
            obj[key] = response.data[i].settingPoints
          }
          this.form = obj
          this.arrnew = response.data.map((item,index) => {
            return Object.assign({},{'id':item.id})
          })
        }
        this.loading = false
      })
    },
    checkInput(num) {
      let str = '';
      str = num
      let len1 = str.substr(0, 1);
      let len2 = str.substr(1, 1);
      //如果第一位是0，第二位不是点，就用数字把点替换掉
      if (str.length > 1 && len1 == 0 && len2 != ".") {
        str = str.substr(1, 1);
      }
      //第一位不能是.
      if (len1 == ".") {
        str = "";
      }
      //限制只能输入一个小数点
      if (str.indexOf(".") != -1) {
        let str_ = str.substr(str.indexOf(".") + 1);
        if (str_.indexOf(".") != -1) {
          str = str.substr(0, str.indexOf(".") + str_.indexOf(".") + 1);
        }
        if (str_.length > 6) {
          this.$message.warning(`小数点后只能输入六位，请正确输入！`);
          return (str = "");
        }
      }
      //正则替换
      str = str.replace(/[^\d^\.]+/g, ""); // 保留数字和小数点
      return str;
    },
    // 提交
    funcSubmit(){
      this.$refs['form'].validate((valid) => {
        if (valid) {
          const rLoading = this.openLoading();
          let arr = []
          for (let i in this.form) {
            let o = {};
            o[i] = this.form[i];
            arr.push(o)
          }
          let resArr = arr.map((item,index) => ({
            settingPoints:Object.values(item)[0],
            settingName:Object.keys(item)[0],
          }))
          var param = resArr.map((item,index) => {
            return {...item, ...this.arrnew[index]};
          });
          pointsSettingSave(param)
          .then(response=> {
            rLoading.close();
            this.$message({ message: response.msg, type: "success" });
            this.from()
          })
          .finally(() => {
            rLoading.close();
          })
        } else {
          return false
        }
      })
    },
  }
}
</script>
<style lang="scss" scoped>
.setBox{
  width: 500px;
  margin: 20px auto;
}
.setBox div{
  margin: 8px auto;
}
.dialog-footer {
  margin: 15px 0 15px 0;
  width: 330px;
  float: right;
}
.el-input {
    width: 200px;
  }
</style>
